{% extends "base.html" %}

{% block title %}图片定制 - 图片定制工具{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2>
        <i class="bi bi-palette me-2"></i>图片定制
    </h2>
    <a href="{{ url_for('intelligent_positioning') }}" class="btn btn-primary">
        <i class="bi bi-plus-circle me-1"></i>新建项目
    </a>
</div>

<div class="row g-4">
    <!-- 现有项目 -->
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="bi bi-folder me-2"></i>我的项目
                </h5>
            </div>
            <div class="card-body">
                {% if projects %}
                    <div class="row g-3">
                        {% for project in projects %}
                        <div class="col-lg-4 col-md-6">
                            <div class="card project-card h-100">
                                <div class="card-body">
                                    <div class="d-flex justify-content-between align-items-start mb-2">
                                        <h6 class="card-title mb-0">{{ project.name }}</h6>
                                        <div class="dropdown">
                                            <button class="btn btn-sm btn-outline-secondary dropdown-toggle" 
                                                    data-bs-toggle="dropdown">
                                                <i class="bi bi-three-dots"></i>
                                            </button>
                                            <ul class="dropdown-menu">
                                                <li>
                                                    <a class="dropdown-item" 
                                                       href="{{ url_for('edit_customization', project_id=project.id) }}">
                                                        <i class="bi bi-pencil me-1"></i>编辑
                                                    </a>
                                                </li>
                                                <li><hr class="dropdown-divider"></li>
                                                <li>
                                                    <a class="dropdown-item text-danger delete-project" href="#" 
                                                       data-project-id="{{ project.id }}" data-project-name="{{ project.name }}">
                                                        <i class="bi bi-trash me-1"></i>删除
                                                    </a>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                    
                                    <p class="card-text text-muted small">
                                        底板: {{ project.template_name }}
                                    </p>
                                    
                                    <div class="project-status mb-2">
                                        {% if project.export_path %}
                                            <span class="badge bg-success">
                                                <i class="bi bi-check-circle me-1"></i>已导出
                                            </span>
                                        {% else %}
                                            <span class="badge bg-warning">
                                                <i class="bi bi-clock me-1"></i>进行中
                                            </span>
                                        {% endif %}
                                    </div>
                                    
                                    <small class="text-muted">
                                        <i class="bi bi-calendar me-1"></i>
                                        {{ project.created_at.split(' ')[0] if project.created_at else '未知' }}
                                    </small>
                                </div>
                                <div class="card-footer bg-transparent">
                                    <div class="d-flex justify-content-between">
                                        <a href="{{ url_for('edit_customization', project_id=project.id) }}" 
                                           class="btn btn-sm btn-primary">
                                            <i class="bi bi-pencil me-1"></i>编辑
                                        </a>
                                        {% if project.export_path %}
                                            <a href="{{ url_for('download_export', filename=project.export_path.split('/')[-1]) }}" 
                                               class="btn btn-sm btn-success">
                                                <i class="bi bi-download me-1"></i>下载
                                            </a>
                                        {% endif %}
                                    </div>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                {% else %}
                    <div class="text-center py-5">
                        <i class="bi bi-folder-plus display-1 text-muted"></i>
                        <h4 class="mt-3 text-muted">还没有定制项目</h4>
                        <p class="text-muted">创建您的第一个图片定制项目</p>
                        <a href="{{ url_for('intelligent_positioning') }}" class="btn btn-primary">
                            <i class="bi bi-plus-circle me-1"></i>新建项目
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
    
    <!-- 底板选择 -->
    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">
                    <i class="bi bi-grid-3x3-gap me-2"></i>可用底板
                </h5>
            </div>
            <div class="card-body">
                {% if templates %}
                    <div class="row g-3">
                        {% for template in templates %}
                        <div class="col-lg-3 col-md-4 col-sm-6">
                            <div class="card template-select-card h-100">
                                <div class="card-body text-center">
                                    <div class="template-preview mb-3">
                                        <div class="preview-box" style="aspect-ratio: {{ template.width }}/{{ template.height }};">
                                            <span class="preview-text">{{ template.width }}×{{ template.height }}</span>
                                        </div>
                                    </div>
                                    <h6 class="card-title">{{ template.name }}</h6>
                                    <p class="card-text text-muted small">
                                        {{ template.description or '暂无描述' }}
                                    </p>
                                </div>
                                <div class="card-footer bg-transparent text-center">
                                    <a href="{{ url_for('intelligent_positioning') }}?template_id={{ template.id }}" 
                                       class="btn btn-sm btn-outline-primary">
                                        <i class="bi bi-palette me-1"></i>使用此底板
                                    </a>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                    </div>
                {% else %}
                    <div class="text-center py-4">
                        <i class="bi bi-grid-3x3-gap display-4 text-muted"></i>
                        <h5 class="mt-3 text-muted">还没有可用底板</h5>
                        <p class="text-muted">请先创建底板裁片</p>
                        <a href="{{ url_for('templates') }}" class="btn btn-primary">
                            <i class="bi bi-grid-3x3-gap me-1"></i>管理底板
                        </a>
                    </div>
                {% endif %}
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_css %}
<style>
.project-card {
    transition: transform 0.2s, box-shadow 0.2s;
}

.project-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.template-select-card {
    transition: transform 0.2s, box-shadow 0.2s;
    cursor: pointer;
}

.template-select-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.template-preview {
    background: #f8f9fa;
    border-radius: 8px;
    padding: 1rem;
}

.preview-box {
    background: white;
    border: 2px dashed #dee2e6;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 80px;
    max-height: 120px;
    position: relative;
}

.preview-text {
    color: #6c757d;
    font-size: 0.75rem;
    font-weight: 500;
}

.project-status .badge {
    font-size: 0.75rem;
}
</style>
{% endblock %}

{% block extra_js %}
<script>
$(document).ready(function() {
    // 删除项目
    $('.delete-project').on('click', function(e) {
        e.preventDefault();
        const projectId = $(this).data('project-id');
        const projectName = $(this).data('project-name');
        
        if (confirm(`确定要删除项目"${projectName}"吗？此操作不可撤销。`)) {
            $.ajax({
                url: `/projects/${projectId}/delete`,
                method: 'POST',
                success: function(response) {
                    if (response.success) {
                        location.reload();
                    } else {
                        alert('删除失败: ' + (response.error || '未知错误'));
                    }
                },
                error: function() {
                    alert('删除失败，请稍后重试');
                }
            });
        }
    });
});
</script>
{% endblock %}
